package com.lh.runmanage.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lh.runmanage.model.InPutParam.MenuHistoryModel;
import com.lh.runmanage.model.InPutParam.MenuJumpInParam;
import com.lh.runmanage.model.MenuJumpModel;
import com.lh.runmanage.model.ResultVO;
import com.lh.runmanage.model.ResultVOPage;
import com.lh.runmanage.service.MenuJumpService;
import com.lh.runmanage.unit.GetPropertiesClass;
import com.lh.runmanage.unit.ResultStruct;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author ：梁昊，后端工程师：梁昊
 * @create 2021-07-22 17:52
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/menuJumpController")
@Api(value = "仅展示界面跳转", description = "系统运行态势平台展示湍")
public class MenuJumpController {
    @Autowired
    MenuJumpService menuJumpService;
    @Autowired
    GetPropertiesClass getPropertiesClass;


    /**
     * 展示端方法：U2， 界面跳转，方法ID：SE202107221749238706FB5A8E67AD3
     *
     * @param projectCode 表menu_jump,字段名projectCode:项目CODE
     * @param createTime 表menu_jump,字段名createTime:数据生成时间
     * @param page 当前页数
     * @param limit 每页条数
     * @return 分页数据
     */
    @ApiOperation(value = "展示端方法：U2， 界面跳转[分页数据],方法ID：SE202107221749238706FB5A8E67AD3", notes = "/menuJumpController/V1/selectMenuJumpByPage?projectCode=projectCode&createTime=createTime&page=page&limit=limit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目CODE", dataType = "String", required = true)
            , @ApiImplicitParam(name = "createTime", value = "数据生成时间", dataType = "Date")
            , @ApiImplicitParam(name = "page", value = "当前页数", dataType = "String")
            , @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "String")
    })
    @PostMapping("/V1/selectMenuJumpByPage")
    public ResultVOPage selectMenuJumpByPage(@RequestParam(value = "projectCode") String projectCode
            , @RequestParam(value = "createTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime
            , @RequestParam(value = "page", defaultValue = "1") int page
            , @RequestParam(value = "limit", defaultValue = "10") int limit) throws UnsupportedEncodingException {
        if (projectCode != null) {
            projectCode = projectCode.trim();
            if (projectCode.getBytes("gbk").length > 100) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：100", "projectCode"), ResultVOPage.class, null);
            }
        }

        if (createTime == null) {
            createTime = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(createTime);
            calendar.add(Calendar.MONTH, -1);
            createTime = calendar.getTime();
        }

        MenuJumpInParam menuJumpInParam = new MenuJumpInParam();
        menuJumpInParam.setProjectCode(projectCode);
        menuJumpInParam.setCreateTime(createTime);

        PageHelper.startPage(page, limit);
        List<MenuJumpModel> menuJumpModels = menuJumpService.selectMenuJumpByPage(menuJumpInParam);
        PageInfo pageInfo = new PageInfo<>(menuJumpModels, limit);
        return ResultStruct.successPage(menuJumpModels, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }
}
